package com.rsscollector.feedrss.parser.parserimpl;

import com.rsscollector.feedrss.parser.GenericParser;
import com.rsscollector.util.IO;
import com.rsscollector.util.StringUtil;
import com.sun.syndication.feed.synd.SyndEntry;

import java.net.URL;
import java.net.MalformedURLException;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

public class DanTriParser extends GenericParser {

    public static final String END_TEXT = "</a>";
    private static Logger logger = Logger.getLogger(VnExpressParser.class.getName());

    public synchronized String getImageLink(String content, String key, SyndEntry matchStr) {
        String result = null;
        String temp = null;
        if (matchStr != null) {
            temp = matchStr.getLink().replaceAll("http://www.dantri.com.vn", "");
            temp += "\"><img";
        }
        if (content != null) {
            int startIndex = content.indexOf(temp);
            while (startIndex >= 0) {
                int endIndex = content.indexOf(END_TEXT, startIndex);
                String sub;
                if (startIndex < endIndex && startIndex >= 0 & endIndex >= 0) {
                    sub = content.substring(startIndex, endIndex);
                    int startSub = 0;
                    int endSub = 0;
                    if ((startSub = sub.indexOf("<img")) >= 0) {
                        endSub = sub.indexOf(">", startSub + 1);
                        result = sub.substring(startSub, endSub + 1);
                    } else {
                        startIndex = content.indexOf(temp, startIndex + 1);
                    }
                }
                if (result != null) {
                    int start = result.indexOf("'");
                    int end = result.indexOf("'", start+1);
                    String str = null;
                    if (start >= 0 && end > start && end > 0) {
                        str = result.substring(start+1, end-1);
                        result = str;
                    }
                    break;
                }
            }
        }

        return result;
    }

    public String getTitle(String title) {
        String result = "";
        int end = title.indexOf("- D");
        if (title != null && end > 1) {
            result = title.substring(0, end-1);
        } else {
            result = title;
        }
        return result.trim();    }

    public StringBuffer getContent(String url) {
        StringBuffer buf = null;
        try {
            String data = IO.getContentFromURL(url);
            if (!StringUtil.isEmpty(data)) {
                int startIndex = data.indexOf("<td class=\"main_subtitle\">");
                int endIndex = data.indexOf("<hr color=\"lightgrey\" size=\"1\">");
                int position = 0;
                if (startIndex >= 0 && endIndex >= 0 && startIndex < endIndex) {
                    data = data.substring(startIndex, endIndex);
                    buf = new StringBuffer(data);

                    // go back 3 </td> to get correct td
//                    for (int i = 0; i < 3; i++) {
//                        buf.lastIndexOf()
//                    }
                    // remove print link
                    startIndex = buf.indexOf("<a href=\"\" onClick=\"return PrintSubject");
                    endIndex   = buf.indexOf("></a>", startIndex);
                    if (startIndex >= 0 && endIndex >= 0 && startIndex < endIndex) {
                        buf.replace(startIndex, endIndex + "></a>".length(), "");
                    }
                    // remove email link
                    startIndex = buf.indexOf("<a href=\"\" onClick=\"return EmailSubject");
                    endIndex   = buf.indexOf("></a>", startIndex);
                    if (startIndex >= 0 && endIndex >= 0 && startIndex < endIndex) {
                        buf.replace(startIndex, endIndex + "></a>".length(), "");
                    }
                    // correct image link
                    position = buf.indexOf("<IMG");
                    while (position > 0) {
                        position = buf.indexOf("src=\"", position);
                        position += "src=\"".length();
                        buf.insert(position, url+"/");
                        position = buf.indexOf("<IMG", position);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("Cannot get content at url:" + url, e);
            /*Don't do anything, because have problem with reading url, we will read this site later*/
        }
        return buf;
    }

}
